package proxy_pattern.remote.ex1;

import java.util.Random;

/**
 * 远程服务类，实现了远程服务接口
 * @author 申书航
 * @version 1.0
 */
public class RemoteService implements RemoteServiceInterface{

    private Random random;

    private static RemoteService service = new RemoteService();

    /**
     * 私有构造函数，初始化随机数生成器
     */
    private RemoteService(){
        random = new Random();
    }

    /**
     * 获取远程服务的实例
     * @return RemoteService 实例
     */
    public static RemoteService getService() {
        return service;
    }

    /**
     * 执行远程功能，返回处理结果
     * @param val 输入值
     * @return 处理结果
     */
    @Override
    public long doRemoteFunction(int val) {
        long waitTime = random.nextInt(1000);
        try {
            Thread.sleep(waitTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return waitTime > 500 ? -1 : val * 10;
    }
}
